package com.xue.web_im;

import com.xue.web_im.netty.CommonServer;
import com.xue.web_im.netty.websocket.WebSocketInitializer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebImApplication implements CommandLineRunner {

    @Value("${netty.port}")
    private int port;

    @Autowired
    private CommonServer commonServer;

    @Autowired
    private WebSocketInitializer webSocketInitializer;

    public static void main(String[] args) {
        SpringApplication.run(WebImApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        ChannelFuture future = commonServer.start(port, webSocketInitializer);
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                commonServer.destroy();
            }
        });
        future.channel().closeFuture().sync();
    }
}
